AddCSLuaFile( 'cl_init.lua' )
AddCSLuaFile( 'shared.lua' )
include( 'shared.lua' )

--Initialize
function ENT:Initialize()
 	--self.BaseClass.Initialize(self)
	--self:SetModel( "models/Combine_Helicopter/helicopter_bomb01.mdl" )
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_VPHYSICS )

	local phys = self:GetPhysicsObject()

	if phys:IsValid() then phys:Wake() end
end

--[[
--updates every frame
function ENT:Think()
	self.BaseClass.Think(self)

	--if !self.Update then return end

	if (WireAddon) then
		if (self._rs.resources) && (!self._rs.alwayson) then self:WireInput( "Active" ) end

		self:WireUpdate( "Active", self.RS.active )

		--max storage values
		for name, value in pairs( self.RS.storage or {} ) do
			self:WireUpdate( "Stored: " .. name, 0 )
			self:WireUpdate( "Max Storage: " .. name, RS.MaxStorage( self, value) )
		end

		--stored values
		for name, value in pairs( self.RS.stored or {} ) do
			self:WireUpdate( "Stored: " .. name, value )
		end
	end


	--MsgN("Update ", self)

	--update the entity in the resource system
	--self:Update()

	self.Entity:NextThink( CurTime() + RS.NodeUpdateTime() )
	return true
end
]]

function ENT:CreateSounds()
	if (self.soundsCreated) then return end

	self.soundsCreated = true

	self.playsounds = {}
	self.stopsounds = {}
	self.forceoffsounds = {}

	for _, sound in pairs(self._rs.startsound or {}) do
		table.insert(self.playsounds, CreateSound(self, Sound(sound)) )	--start sound

		MsgN("start sound: " .. sound)
	end

	self.forceoffsound = CreateSound(self, Sound("buttons/combine_button2.wav"))

	table.insert(self.forceoffsounds,  self.forceoffsound)	--force off sound


	for _, sound in pairs(self._rs.stopsound or {}) do
		table.insert(self.stopsounds, CreateSound(self, Sound(sound)) )	--stop sound

		MsgN("stop sound: " .. sound)
	end
end

function ENT:TurnOn( ply )
	self.BaseClass.Think(self)

	if (self:IsActive()) then return end


	--make sure sounds are created
	self:CreateSounds()


	local requires = self:GetRequirements()
	local stored = self:NodeStoredAmount()

		--check requirements the entity may have
		for name, value in pairs( requires or {} ) do
			--if device has requirements, and requirements are not meet, dont turn on
			if (-value > (stored[name] or 0)) then

				MsgN("forceoff")

				for _, sound in pairs(self.forceoffsounds or {}) do
					sound:PlayEx(1, 100)

					timer.Simple(0.5, function(sound)
						sound:Stop()
					end, sound)
				end

				return
			end
		end

	--set active
	self:SetActive()

	--stop sounds
	for _, sound in pairs(self.stopsounds or {}) do
		sound:Stop()	--stop sound
	end

	--play sounds
	for _, sound in pairs(self.playsounds or {}) do
		sound:PlayEx(0.5, 100)	--start sound
	end

	--force update
	self:Update()
--[[
	--update wire
	if (WireAddon) && (self.WireOutputs) && (table.HasValue(self.WireOutputs, "Active")) then
		self:WireUpdate( "Active", self.RS.active )
	end
]]
end

function ENT:TurnOff( ply, forceoff )
	if (!self:IsActive()) then return end

	--make sure sounds are created
	self:CreateSounds()

	--set inactive
	self:SetInActive()

	--stop sounds
	for _, sound in pairs(self.stopsounds or {}) do
		sound:PlayEx(0.5, 100) --play sound
	end

	--play sounds
	for _, sound in pairs(self.playsounds or {}) do
		sound:Stop() --stop sound
	end

	--force update
	self:Update()
--[[
	--update wire
	if (WireAddon) && (self.WireOutputs && table.HasValue(self.WireOutputs, "Active")) then
		self:WireUpdate( "Active", self.RS.active )
	end
]]
end

function ENT:TriggerInput( name, value )
	if (name == "Active") then
		if (value == 0) then
			self:TurnOff()
		else
			self:TurnOn()
		end
	end

	return false
end

function ENT:AcceptInput(name, activator, caller)
	if (name == "Use") && (caller:IsPlayer()) && (caller:KeyDownLast(IN_USE) == false) then
		if (self:IsActive()) then
			self:TurnOff(caller)
		else
			self:TurnOn(caller)
		end
	end

	return false
end

function ENT:PreEntityCopy()
	RS.BuildDupeInfo( self )
end

function ENT:PostEntityPaste( ply, ent, CreatedEntities )
	RS.ApplyDupeInfo(ply, ent, ent.EntityMods.RSDupeInfo, function(id) return CreatedEntities[id] end)
end

function ENT:OnRemove()
	self:TurnOff()
--[[
	--update wire
	if (WireAddon) then Wire_Remove(self.Entity) end
]]
end

function ENT:OnTakeDamage( dmg )
	self.BaseClass.OnTakeDamage( self, dmg )

	TAKEDAMAGE(self, dmg) --rs systems damage handling

	if (RS.GetHealth(self) <= 0) then
		self.Entity:SetColor( 255, 0, 0, 255 ) --dark when off
	end
end

function ENT:Destruct()
	RS.ENT:Destruct( self )
end

--[[
function ENT:SetTitle( title )
	--save title to prevent unneeded updates
	if (self.title) && (title == self.title) then
	--	return
	end

	umsg.Start("RS.SetEntTitle")
	umsg.Entity( self )
	umsg.String( title )
	umsg.End()

	self.title = title
end
]]

--[[Additional functions
function ENT:AcceptInput( name ) end
function ENT:EndTouch( ent ) end
function ENT:KeyValue( key, value ) end
function ENT:OnRestore() end
function ENT:PhysicsCollide( physobj ) end
function ENT:PhysicsSimulate( phys, deltatime ) end
function ENT:PhysicsUpdate( phys ) end
function ENT:StartTouch( ent ) end
function ENT:Touch( entity ) end
function ENT:UpdateTransmitState() end

function ENT:Use( activator, caller )
	return false
end
]]

